package com.chase.springboot.controller;

import com.chase.springboot.model.Menu;
import com.chase.springboot.model.Role;
import com.chase.springboot.model.User;
import com.chase.springboot.service.RoleMenuService;
import com.chase.springboot.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("用户-角色管理")
@RestController
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("查询用户拥有的角色")
    @GetMapping("/user-role/{userId}")
    public List<Role> getRolesByUserId(@PathVariable("userId")Integer userId){
        return userRoleService.getRolesByUserId(userId);
    }
    @ApiOperation("添加用户拥有的角色")
    @PostMapping("/user-role/{userId}/{roleId}")
    public Boolean addRoleToUser(@PathVariable("userId")Integer userId,
                                 @PathVariable("roleId")Integer roleId){
        return userRoleService.addRoleToUser(roleId, userId);
    }
    @ApiOperation("删除用户拥有的角色")
    @DeleteMapping("/user-role/{userId}")
    public Boolean deleteMenuInRole(@PathVariable("userId")Integer userId){
        return userRoleService.deleteRoleByUserId(userId);
    }



    @ApiOperation("查询角色下的用户")
    @GetMapping("/role-user/{roleId}")
    public List<User> getUsersByRoleId(@PathVariable("roleId")Integer roleId){
        return userRoleService.getUsersByRoleId(roleId);
    }
    @ApiOperation("添加角色下的用户")
    @PostMapping("/role-user/{roleId}/{userId}")
    public Boolean addUserToRole(@PathVariable("roleId")Integer roleId,
                                 @PathVariable("userId")Integer userId){
        return userRoleService.addUserToRole(userId, roleId);
    }
    @ApiOperation("删除角色下的用户")
    @DeleteMapping("/role-user/{roleId}")
    public Boolean deleteUserByRoleId(@PathVariable("roleId")Integer roleId){
        return userRoleService.deleteUserByRoleId(roleId);
    }


    @ApiOperation("查询用户有权限的菜单")
    @GetMapping("/user-role-menu/{userId}")
    public List<Menu> getMenuByUserId(@PathVariable("userId")Integer userId){
        return userRoleService.getMenusByUserId(userId);
    }
}
